<?php
class Calls_model extends CRM_Model{
	private $typeDocRid;
	public function __construct(){
		parent::__construct();
		$typeDocRow = $this->GetTypeDoc();
		$this->typeDocRid = $typeDocRow->rid; 
	}
	
	public function GetDS(){
		$this->db->select('_documents.rid, 
							DATE_FORMAT(_calls_headers.date_doc, \'%d.%m.%Y\') as date_doc, 
							_calls_headers._callsstatuses_rid as _callsstatuses_rid,
							_callsstatuses.callsstatuses_name as call_status,
							_calls_rows._countries_rid as _countries_rid,
							_countries.country_name as country_name,
							_calls_rows._callhotelcats_rid as _callhotelcats_rid,
							_callhotelcats.callhotelcat_name as hotel_cat,
							_calls_rows._clients_rid  as _clients_rid ,
							_calls_rows._advertisessources_rid as _advertisessources_rid,
							_advertisessources.source_name as source_name,
							_calls_rows._currencies_rid as _currencies_rid,
							_currencies.code as currency_code,
							_calls_rows.f_name as f_name,
							_calls_rows.s_name as s_name,
							_calls_rows.l_name as l_name,
							DATE_FORMAT(_calls_rows.date_from, \'%d.%m.%Y\') as date_from,
							DATE_FORMAT(_calls_rows.date_to, \'%d.%m.%Y\') as date_to,
							_calls_rows.summ_wanted_from as summ_wanted_from,
							_calls_rows.summ_wanted_to as summ_wanted_to,
							_calls_rows.phones as phones,
							_calls_rows.email as email,
							DATE_FORMAT(_documents.modifyDT, \'%d.%m.%Y %H:%i\') as modifyDT, 
							_documents.descr as descr, _documents.archive
							');
		$this->db->from('_documents');
		$this->db->join('_calls_headers', '_calls_headers._documents_rid = _documents.rid');
		$this->db->join('_calls_rows', '_calls_rows._calls_headers_rid = _calls_headers.rid');
		$this->db->join('_callsstatuses', '_calls_headers._callsstatuses_rid = _callsstatuses.rid');
		$this->db->join('_countries', '_calls_rows._countries_rid = _countries.rid');
		$this->db->join('_callhotelcats', '_calls_rows._callhotelcats_rid = _callhotelcats.rid');
		$this->db->join('_advertisessources', '_calls_rows._advertisessources_rid = _advertisessources.rid');
		$this->db->join('_currencies', '_calls_rows._currencies_rid = _currencies.rid');
		$this->db->where(array('_documents._documents_types_rid'=>$this->typeDocRid));
		if($searchRule = $this->ciObject->GetSessionData('searchrule')){
			$whereArr = array();
			if(isset($searchRule['_calls_headers.date_doc>='])) { $whereArr['_calls_headers.date_doc>='] = $this->toMySQLDate($searchRule['_calls_headers.date_doc>=']); unset($searchRule['_calls_headers.date_doc>=']); } 
			if(isset($searchRule['_calls_headers.date_doc<='])) { $whereArr['_calls_headers.date_doc<='] = $this->toMySQLDate($searchRule['_calls_headers.date_doc<=']); unset($searchRule['_calls_headers.date_doc<=']); } 
			//var_dump($searchRule);
			if($whereArr)  $this->db->where($whereArr);
			$this->db->like($searchRule);
		}
		if($sort = $this->ciObject->GetSessionData('sort')){
			$this->db->orderby($sort['c'], $sort['r']);
		}
		$query = $this->DBGet('_documents');
		if($query->num_rows()>0) return $this->ciObject->GetPagingDS($query->result());
		return array();
	}
	
	public function GetCallsstatusesList(){
		$this->db->select('_callsstatuses.rid, callsstatuses_name as callsstatuses_name');
		$this->db->from('_callsstatuses');
		$this->db->orderby('callsstatuses_name');
		$query = $this->DBGet('_callsstatuses');
		if($query->num_rows()>0) return $query->result();
		return False;
	}
	
	public function GetCountriesList(){
		$this->db->select('_countries.rid, _countries.country_name as country_name');
		$this->db->from('_countries');
		$this->db->orderby('country_name');
		$query = $this->DBGet('_countries');
		if($query->num_rows()>0) return $query->result();
		return False;
	}
	
	public function GetCallhotelcatsList(){
		$this->db->select('_callhotelcats.rid, _callhotelcats.callhotelcat_name as callhotelcat_name');
		$this->db->from('_callhotelcats');
		$this->db->orderby('callhotelcat_name');
		$query = $this->DBGet('_callhotelcats');
		if($query->num_rows()>0) return $query->result();
		return False;
	}

	public function GetCurrenciesList(){
		$this->db->select('_currencies.rid, _currencies.code as code');
		$this->db->from('_currencies');
		$this->db->orderby('code');
		$query = $this->DBGet('_currencies');
		if($query->num_rows()>0) return $query->result();
		return False;
	}
	
	public function GetAdvertisesList(){
		$this->db->select('_advertises_rows.rid, _advertises_rows.advertises_name as advertises_name');
		$this->db->from('_advertises_rows');
		$this->db->orderby('advertises_name');
		$query = $this->DBGet('_advertises_rows');
		if($query->num_rows()>0) return $query->result();
		return False;
	}

	public function GetEditDS($p_docRid = null){
		$this->db->select('DATE_FORMAT(_calls_headers.date_doc, \'%d.%m.%Y\') as date_doc,
							_calls_headers.rid as _calls_headers_rid,
							_calls_headers._callsstatuses_rid as _callsstatuses_rid,
							_calls_rows._countries_rid as _countries_rid,
							_calls_rows._callhotelcats_rid as _callhotelcats_rid,
							_calls_rows._clients_rid  as _clients_rid ,
							_calls_rows._advertisessources_rid as _advertisessources_rid,
							_advertisessources.source_name as source_name,
							_calls_rows._currencies_rid as _currencies_rid,
							_calls_rows.f_name as f_name,
							_calls_rows.s_name as s_name,
							_calls_rows.l_name as l_name,
							DATE_FORMAT(_calls_rows.date_from, \'%d.%m.%Y\') as date_from,
							DATE_FORMAT(_calls_rows.date_to, \'%d.%m.%Y\') as date_to,
							_calls_rows.summ_wanted_from as summ_wanted_from,
							_calls_rows.summ_wanted_to as summ_wanted_to,
							_calls_rows.tourists_wish as tourists_wish,
							_calls_rows.tourists_quan as tourists_quan,
							_calls_rows.tourists_offers as tourists_offers,
							_calls_rows.tourists_answers as tourists_answers,
							_calls_rows.phones as phones,
							_calls_rows.email as email,
							DATE_FORMAT(_documents.modifyDT, \'%d.%m.%Y %H:%i\') as modifyDT, 
							_documents.descr as descr, _documents.archive
							');
		$this->db->from('_documents');
		$this->db->join('_calls_headers', '_calls_headers._documents_rid = _documents.rid');
		$this->db->join('_calls_rows', '_calls_rows._calls_headers_rid = _calls_headers.rid');
		$this->db->join('_advertisessources', '_calls_rows._advertisessources_rid = _advertisessources.rid');
		$this->db->where(array('_documents.rid'=>($p_docRid)?$p_docRid:$this->ciObject->GetSessionData('activerecord')));
		$query = $this->DBGet('_documents');
		if($query->num_rows()>0) return $query->row();
		return False;
	}
	
	public function CreateRecord(){
		$insDocumentArray = array('_documents_types_rid'=>$this->typeDocRid, 
									'descr'=>$_POST['descr'],
									'modifyDT'=>$this->nowTime,
									'modifier_users_rid'=>$this->userRid);
		$this->db->trans_begin();
		$this->db->insert('_documents', $insDocumentArray);
		$docRid = $this->db->insert_id();
		$insHeaderArray = array('date_doc'=>$this->toMySQLDate($_POST['date_doc']),
							'_documents_rid'=>$docRid,
							'_callsstatuses_rid'=>$_POST['_callsstatuses_rid'],
							
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);

		$this->db->insert('_calls_headers', $insHeaderArray);
		$insRowsArray = array('_calls_headers_rid'=>$this->db->insert_id(),
							'_countries_rid'=>$_POST['_countries_rid'],
							'_callhotelcats_rid'=>$_POST['_callhotelcats_rid'],
							'_advertisessources_rid'=>$_POST['_advertisessources_rid'],
							'_currencies_rid'=>$_POST['_currencies_rid'],
							'f_name'=>$_POST['f_name'],
							's_name'=>$_POST['s_name'],
							'l_name'=>$_POST['l_name'],
							'date_from'=>$this->toMySQLDate($_POST['date_from']),
							'date_to'=>$this->toMySQLDate($_POST['date_to']),
							'summ_wanted_from'=>$_POST['summ_wanted_from'],
							'summ_wanted_to'=>$_POST['summ_wanted_to'],
							'tourists_wish'=>$_POST['tourists_wish'],
							'tourists_quan'=>$_POST['tourists_quan'],
							'tourists_offers'=>$_POST['tourists_offers'],
							'tourists_answers'=>$_POST['tourists_answers'],
							'phones'=>$_POST['phones'],
							'email'=>$_POST['email'],
							
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);
		$this->db->insert('_calls_rows', $insRowsArray);
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return $docRid;
		}		
	}
	
	public function UpdateRecord(){
		$updateDocumentArray = array('_documents_types_rid'=>$this->typeDocRid, 
									'descr'=>$_POST['descr'],
									'modifyDT'=>$this->nowTime,
									'modifier_users_rid'=>$this->userRid);
		$this->db->trans_begin();
		$this->db->update('_documents', $updateDocumentArray, array('_documents.rid'=>$this->ciObject->GetSessionData('activerecord')));
				
		$updateHeaderArray = array('_callsstatuses_rid'=>$_POST['_callsstatuses_rid'],
								'archive'=>$_POST['archive'],
								'modifier_users_rid'=>$this->userRid,
								'modifyDT'=>$this->nowTime);

		$this->db->update('_calls_headers', $updateHeaderArray, array('_calls_headers._documents_rid'=>$this->ciObject->GetSessionData('activerecord')));
		$query = $this->db->getwhere('_calls_headers', array('_calls_headers._documents_rid'=>$this->ciObject->GetSessionData('activerecord')));
		if(!$query->num_rows()) {
			$this->db->trans_rollback();
    		return False;
		}
		$row = $query->row();
		$updateRowsArray = array('_countries_rid'=>$_POST['_countries_rid'],
							'_callhotelcats_rid'=>$_POST['_callhotelcats_rid'],
							'_advertisessources_rid'=>$_POST['_advertisessources_rid'],
							'_currencies_rid'=>$_POST['_currencies_rid'],
							'f_name'=>$_POST['f_name'],
							's_name'=>$_POST['s_name'],
							'l_name'=>$_POST['l_name'],
							'date_from'=>$this->toMySQLDate($_POST['date_from']),
							'date_to'=>$this->toMySQLDate($_POST['date_to']),
							'summ_wanted_from'=>$_POST['summ_wanted_from'],
							'summ_wanted_to'=>$_POST['summ_wanted_to'],
							'tourists_wish'=>$_POST['tourists_wish'],
							'tourists_quan'=>$_POST['tourists_quan'],
							'tourists_offers'=>$_POST['tourists_offers'],
							'tourists_answers'=>$_POST['tourists_answers'],
							'phones'=>$_POST['phones'],
							'email'=>$_POST['email'],
							
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);
		$this->db->update('_calls_rows', $updateRowsArray, array('_calls_rows._calls_headers_rid'=>$row->rid));
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return $this->ciObject->GetSessionData('activerecord');
		}		
	}
	
	public function DeleteRecord(){
		$this->db->trans_begin();
		$this->db->delete('_documents', array('rid'=>$this->ciObject->GetSessionData('activerecord')));
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return True;
		}		
	}
	
}


?>